/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ifrr.gepro.sprint.handler;

import br.ifrr.gepro.estoria.bean.EstoriasBean;
import br.ifrr.gepro.projeto.bean.ProjetoBean;
import br.ifrr.gepro.projeto.dao.ProjetoDao;
import br.ifrr.gepro.sprint.bean.SprintBean;
import br.ifrr.gepro.sprint.dao.SprintDao;
import br.ifrr.gepro.util.DAOException;
import br.ifrr.gepro.util.JsfUtil;
import br.ifrr.gepro.util.Navegacao;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author rafael
 */
@ManagedBean(name = "sprint")
@SessionScoped
public class SprintMB {

    private SprintBean novosprint = new SprintBean();
    private ArrayList<SprintBean> listaSprint = new ArrayList<SprintBean>();
    private HtmlSelectOneMenu projetoSelecionado = new HtmlSelectOneMenu();
    

    public String preparaCadastro() {
        novosprint = new SprintBean();
        return Navegacao.paginaCadSprint;
    }

    public String salvar() throws DAOException {
        novosprint.setFkProj(((ProjetoBean) new ProjetoDao().selecionar(Integer.parseInt(projetoSelecionado.getValue().toString()))));
        if (novosprint.getIdSprint() == null) {
            new SprintDao().inserir(getNovosprint());
        } else {
            new SprintDao().alterar(getNovosprint());
        }

        JsfUtil.showMessageInfo("Cadastro realizado com sucesso!");
        return Navegacao.paginaEstSprint;
    }
     
    /**
     * @return the novosprint
     */
    public String editar() throws DAOException {

        int s = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("idSprint"));
        novosprint = (SprintBean) new SprintDao().selecionar(s);
        return Navegacao.paginaAtuSprint;
    }

    public String excluir() throws DAOException {
        int s = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("idSprint"));
        new SprintDao().excluir(new SprintBean(s));
        JsfUtil.showMessageInfo("Operação realizada com sucesso!");
        return null;
    }

    public SprintBean getNovosprint() {
        return novosprint;
    }

    /**
     * @param novosprint the novosprint to set
     */
    public void setNovosprint(SprintBean novosprint) {
        this.novosprint = novosprint;
    }

    public ArrayList<SprintBean> getListaSprint() throws DAOException {
        listaSprint = ((ArrayList<SprintBean>) new SprintDao().listar());
        return listaSprint;
    }

    public void setListaSprint(ArrayList<SprintBean> listaSprint) {
        this.listaSprint = listaSprint;
    }

    public HtmlSelectOneMenu getProjetoSelecionado() {
        return projetoSelecionado;
    }

    public void setProjetoSelecionado(HtmlSelectOneMenu projetoSelecionado) {
        this.projetoSelecionado = projetoSelecionado;
    }

    public List<SprintBean> getSprints() throws DAOException {
        return new SprintDao().listar();
    }

    public List<SelectItem> getSprintsCombo() throws DAOException {
        List<SelectItem> lista = new ArrayList<SelectItem>();
        for (SprintBean s : getSprints()) {
            lista.add(new SelectItem(s.getIdSprint(), s.getNome()));
        }
        return lista;
    }
}
